package leetcode.weekly.week326;

import java.util.HashSet;
import java.util.Set;

//Solution2Test
public class Solution2 {

	public int distinctPrimeFactors(int[] nums) {
		Set<Integer> prime = new HashSet<>();
		for (int i = 2; i < 1001; i++) {
			if (isPrime(i)) {
				prime.add(i);
			}
		}

		Set<Integer> ans = new HashSet<>();
		for (int num : nums) {
			for (int i = 2; i <= num && num > 1; i++) {
				while (num % i == 0) {
					if (prime.contains(i)) {
						ans.add(i);
					}
					num /= i;
				}
			}
		}
		return ans.size();
	}

	public boolean isPrime(int num) {
		if (num == 2) {
			return true;
		}
		if (num == 1 || num % 2 == 0) {
			return false;
		}
		for (int i = 3; i * i <= num; i += 2) {
			if (num % i == 0 || i * i == num) {
				return false;
			}
		}
		return true;
	}

}
